﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyBanHang.BLL;
using QuanLyBanHang.DTO;
using QuanLyBanHang.QLLayThongTin;
using QuanLyBanHang.DoiTuongLayThongTin;


namespace QuanLyBanHang
{
    public partial class frmChiTietHoaDon : Form
    {

        KhachHangDTO kh = new KhachHangDTO();
        ChiTietHDBLL cthd = new ChiTietHDBLL();
        SanPhamcs sp = new SanPhamcs();
        DataTable dtKH = new DataTable();
        DataTable dtSP = new DataTable();
      
        public frmChiTietHoaDon()
        {
            InitializeComponent();

        }
    
        private void frmChiTietHoaDon_Load(object sender, EventArgs e)
        {
            
            KhachHangCBB();
            SanPHamCBB();

            textBox1.Text = DoiTuongHD.maHD;
            textBox2.Text = DoiTuongHD.maKH;
           
           
          


        }
        public void TaoTTHD()
        {
            textBox1.Text = "";
            comboBox1.Text = "";
            maskedTextBox1.Text = "";
            textBox4.Text = "";
            dateTimePicker1.Text = "";
        }
       


     

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát hay không ", "ThôngBáo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void txtDonGia_TextChanged(object sender, EventArgs e)
        {
            TinhThanhTien();

        }
        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            TinhThanhTien();
        }
        //tinh thanh tien

        private void TinhThanhTien()
        {
            int soluong = 0;
            int dongia = 0;
            int.TryParse(txtSoLuong.Text, out soluong);
            int.TryParse(txtDonGia.Text, out dongia);
            int thanhtien = soluong * dongia;
            txtThanhTien.Text = thanhtien.ToString();
            txtThanhTien.ReadOnly = true;
            //txtThanhTien.ForeColor = Color.Red;
        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


            //string maKH = comboBox1.SelectedValue.ToString();
            //textBox1.Text = KhachHangBLL.LayDuLieuMOtCot("MaKH");

            //dtKH = KhachHangBLL.LayTatCaThongTinKhachHang(kh);

            //DataRow[] kq = dtKH.Select("MaKH = " + comboBox1.SelectedValue);
            //foreach (DataRow row in kq)
            //{
            //    row["DienThoai"] = Convert.ToInt32(textBox1.Text);
            //}

            //MessageBox.Show(comboBox1.SelectedIndex+":"+comboBox1.SelectedValue);

            //if (comboBox1.SelectedValue.GetType() != typeof( DataRowView))
            //{
            //    //MessageBox.Show(comboBox1.SelectedIndex+":"+comboBox1.SelectedValue);


            //}
            //DataRow[] kq = dtKH.Select("MaKH = '" + comboBox1.SelectedValue + "'");
            //foreach (DataRow row in kq)
            //{
            //    textBox2.Text = row["DienThoai"].ToString();
            //}
        }
        #region Su kien CBB
        private void KhachHangCBB()
        {
            

            dtKH = KhachHangBLL.LayTatCaThongTinKhachHang(kh);
            comboBox1.DataSource = dtKH;
            comboBox1.DisplayMember = "HoTenKH";
            comboBox1.ValueMember = "MaKH";

           
        }
       
      
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {

            DataRow[] kq = dtKH.Select("MaKH = '" + comboBox1.SelectedValue + "'");
            foreach (DataRow row in kq)
            {
                 
                 maskedTextBox1.Mask = "999-000-0000";
                 maskedTextBox1.Text = row["DienThoai"].ToString();
               
            }

        }
        private void SanPHamCBB()
        {
            
            dtSP = SanPhamBLL.LayTatCaSanPham(sp);
            cbb_MaSP.DataSource = dtSP;
            cbb_MaSP.DisplayMember = "MaSP";
            cbb_MaSP.ValueMember = "MaSP";
            
            //lay ten san pham 
            comboBox2.DataSource = dtSP;
            comboBox2.DisplayMember = "TenSP";
            comboBox2.ValueMember = "MaSP";
        }
       
        
        private void comboBox2_SelectedValueChanged_1(object sender, EventArgs e)
        {

            DataRow[] kq = dtSP.Select("MaSP = '" + comboBox2.SelectedValue + "'");
            foreach (DataRow row in kq)
            {

                txtDVT.Text = row["DonViTinh"].ToString();

            }
        }
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == cbb_MaSP.Index)
                {
                    combo.SelectedIndexChanged -= combo_ConfirmSelectionChange;

                    combo.SelectedIndexChanged += combo_ConfirmSelectionChange;
                    return;
                }
            }
            //if (dataGridView1.row[])
            //{
                
            //}
            //if (dataGridView1.CurrentCell.ColumnIndex == cbb_MaSP.Index && e.Control is ComboBox)
            //{
            //    //ComboBox comboBox = e.Control as ComboBox;
            //    comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged;
            //    comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
            //} 
        }

        private void combo_ConfirmSelectionChange(object sender, EventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            if (dataGridView1.CurrentCell.ColumnIndex != cbb_MaSP.Index) return;
            if (combo == null) return;
            if (combo != null)
            {
                
                DataRow[] kq = dtSP.Select("MaSP = '" + combo.SelectedValue + "'");
                foreach (DataRow row in kq)
                {
                    dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["TenSanPham"].Value = row["TenSP"].ToString();
                    dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["col_DonGia"].Value = row["DonGia"].ToString();
                   

                }
            }
        }

       

    

     

        


        

        //private void LastColumnComboSelectionChanged(object sender, EventArgs e)
        //{
        //    MessageBox.Show("aa");
        //    ComboBox combobox = (ComboBox)sender;
        //    DataRow[] kq = dtSP.Select("MaSP = '" +combobox.SelectedValue+ "'");
        //    foreach (DataRow row in kq)
        //    {
        //        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Column1"].Value = row["TenSP"].ToString();
        //        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["col_DonGia"].Value = row["DonGia"].ToString();

        //    }
        //}

        //sử dụng EditingControlShowing sự kiện cho nó
        //private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        //{
        //    if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name.Equals("cbb_MaSP"))
        //    {
        //        ComboBox cmbprocess = e.Control as ComboBox;
        //        cmbprocess.SelectedIndexChanged += new EventHandler(cmbprocess_SelectedIndexChanged);
        //    }
        //}


        //private void cmbprocess_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ComboBox cmbprocess = (ComboBox)sender;
        //    if (cmbprocess.SelectedValue != null)
        //    {
        //        MessageBox.Show("aa");
        //        ComboBox combobox = (ComboBox)sender;
        //        DataRow[] kq = dtSP.Select("MaSP = '" + cmbprocess.SelectedValue + "'");
        //        foreach (DataRow row in kq)
        //        {
        //            dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Column1"].Value = row["TenSP"].ToString();
        //            dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["col_DonGia"].Value = row["DonGia"].ToString();

        //        }

        //    }

        //}

        //    if (e.control is combobox)
        //    {

        //               ((combobox)e.control).selectedindexchanged += new system.eventhandler(this.onseleteddatagridomboboxeveznt);

        //    }
        //private void onseleteddatagridomboboxevent(object sender, eventargs e)
        //    {

        //            try
        //            {
        //                string test = datagridview.rows[0].cells["c2"].value.tostring();
        //                     datatable dt = pdal.get_data("select  date from tbl_packet where pno='" + test.tostring() + "'");
        //                      d = dt.rows[0][0].tostring();
        //                      grd_setter.rows[0].cells["c3"].value = d.tostring();
        //            }
        //            catch { }
        //    }
        #endregion
        //them hoa don 
        private void button1_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.CreateCells(dataGridView1);
           // row.Cells["STT"].Value = dataGridView1.RowCount ;
            // row.Cells["cbb_MaSP"].Value = comboBox1.SelectedValue.ToString();
            // row.Cells["TenSanPham"].Value = cbb_MaSP;
            row.Cells["col_SoLuong"].Value = txtSoLuong.Text;
            row.Cells["col_DonGia"].Value = txtDonGia.Text;
            row.Cells["col_ThanhTien"].Value = txtThanhTien.Text;


            //them vao gridview
            dataGridView1.Rows.Add(row);

        }
        //tinh thanh tien tren datagridview
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            decimal qty = 0;
            decimal price = 0;
           
            //if (dataGridView1.CurrentRow.Cells["SoLuong"].Value != null && dataGridView1.CurrentRow.Cells["SoLuong"].Value.ToString().Trim() != "")
            //{
            //    qty = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["SoLuong"].Value);
            //}
            //if (dataGridView1.CurrentRow.Cells["DonGiaBan"].Value != null && dataGridView1.CurrentRow.Cells["DonGiaBan"].Value.ToString().Trim() != "")
            //{
            //    price = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["DonGia"].Value);
            //}
            qty = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["col_SoLuong"].Value);
            price = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["col_DonGia"].Value);
            dataGridView1.CurrentRow.Cells["col_ThanhTien"].Value = qty * price;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        } 

   }


      

      

       

    }
